//
//  ANUserQuickLogonViewController.m
//  Ants
//
//  Created by ZHM on 16/8/8.
//  Copyright © 2016年 ZHM. All rights reserved.
//

#import "ANUserQuickLogonViewController.h"

#define ANLoginOKNote @"loginOKNote"


@interface ANUserQuickLogonViewController ()

@end

@implementation ANUserQuickLogonViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title=@"快速注册";
    [self.landBtn setTitle:@"登录" forState:UIControlStateNormal];
}
//手机快速注册登录
-(void)setLoginBtnLogic{
    if ([phone.text isEqualToString:@""])
    {
        [SVProgressHUD showInfoWithStatus:@"亲,请输入注册手机号码"];
        return;
    }
    else if (phone.text.length <11)
    {
        [SVProgressHUD showInfoWithStatus:@"您输入的手机号码格式不正确"];
        return;
    }
    else if ([code.text isEqualToString:@""])
    {
        [SVProgressHUD showInfoWithStatus:@"亲,请输入验证码"];
        return;
    }
    //获取手机号、验证码
    NSString *mobilePhoneNumber = phone.text;
    NSString *smsCode = code.text;
    
    //该方法可以进行注册和登录两步操作，如果已经注册过了就直接进行登录
//    [BmobUser signOrLoginInbackgroundWithMobilePhoneNumber:mobilePhoneNumber andSMSCode:smsCode block:^(BmobUser *user, NSError *error) {
//        if (user) {
//            //跳转
//            NSLog(@"新账号登录成功!!");
//            //跳转界面
//            [self.navigationController popToRootViewControllerAnimated:YES];
//            [[NSNotificationCenter defaultCenter] postNotificationName:ANLoginOKNote object:nil];
//            [sharedWebService fetchUserBasicInfoWithCompletion:nil];
//        } else {
//            NSLog(@"%@",error);
//            UIAlertView *tip = [[UIAlertView alloc] initWithTitle:nil message:@"验证码有误" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
//            [tip show];
//        }
//    }];
    [sharedWebService registeredWithName:mobilePhoneNumber passWord:smsCode completion:^(BmobUser *user, BOOL succeeded, NSError *error) {
        if (succeeded) {
            NSLog(@"新账号注册成功!!");
            //注册成功-登录
            [sharedWebService loginWithAccount:user.username passWord:user.password completion:^(BmobUser *user, BOOL succeeded, NSError *error) {
                NSLog(@"新账号登录成功!!");
                //跳转界面
                [self.navigationController popToRootViewControllerAnimated:YES];
                [[NSNotificationCenter defaultCenter] postNotificationName:ANLoginOKNote object:nil];
            }];
        } else {
            NSLog(@"注册失败:%@!!",error);
        }
    }];
}

-(void)next {
    [self setLoginBtnLogic];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
